package edu.sctu;



import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDataBaseImpl implements DataBase<User> {
    private final Connection connection=createConnection();
    private Statement statement =null;
    private ResultSet resultSet=null;

    @Override
    public List<User> queryFor(User user) {
        List<User> list = new ArrayList<>();

        try {
           statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql ="";
        if (user.getId() != 0){
            sql = "select * from t_user2 where id="+user.getId()+"";
        }else {
            if (user.getName()!=null&&user.getPassword()==null){
                sql = "select * from t_user2 where username='"+user.getName()+"'";
            }else if (user.getName()!=null&&user.getPassword()!=null){
                sql = "select * from t_user2 where username='"+user.getName()+"' and password='"+user.getPassword()+"'";
            }
        }
        try {
         resultSet= statement.executeQuery(sql);
            while (resultSet.next()){
               User user1 =new User();
                user1.setId(resultSet.getInt(1));
                user1.setName(resultSet.getString(2));
                user1.setPassword(resultSet.getString(3));
                list.add(user1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Boolean updateFor(User user) {
        try {
            statement = connection.createStatement();
            if (user.getId() ==0){
                throw new IllegalArgumentException("id为空");
            }
            String sql = "UPDATE t_user2 set username='"+user.getName()+"', password='"+user.getPassword()+"' WHERE id="+user.getId();
            boolean success =  statement.execute(sql);
            return  success;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean deleteFor(String id) {
        try {
            statement = connection.createStatement();
            String sql = "DELETE FROM t_user2 WHERE  id="+id;
            boolean success =  statement.execute(sql);
            return  success;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean addFor(User user) {
        try {
            statement = connection.createStatement();
            String sql = "insert into t_user2(id,username,password) values("
                    +user.getId()+",'"+user.getName()+"','"+user.getPassword()+"')";
            boolean success =  statement.execute(sql);
            return  success;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean toLogin(User user) {
        int size = queryFor(user).size();
        return size==1;
    }
}
